package com.bawei.firm

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager


import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.bean.HouseEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.firm.databinding.ActivityFirmBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/firm/FirmActivity")
class FirmActivity : BaseActivity<ActivityFirmBinding,FirmViewModel>() {
    lateinit var firmAdapter: FirmAdapter
    val frimList= mutableListOf<HouseEntity>()
    override fun initView() {
        viewModel.frim()
        firmAdapter= FirmAdapter()
        binding.pirmRec.adapter= firmAdapter
        binding.pirmRec.layoutManager=LinearLayoutManager(this)
        binding.pirmSearch.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                firmAdapter.data.clear()
                frimList.forEach {
                    if (it.enterpriseName.contains(query!!)){
                        firmAdapter.data.add(it)
                    }
                    firmAdapter.notifyDataSetChanged()
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {

              if (newText.isNullOrEmpty()){
                  firmAdapter.data.clear()
                  firmAdapter.data.addAll(frimList)
                  firmAdapter.notifyDataSetChanged()
              }
                return true
            }
        })
    }

    override fun initData() {
        viewModel.frimSuccess.observe(this){
            frimList.clear()
            frimList.addAll(it)
            firmAdapter.data.clear()
            firmAdapter.data.addAll(it)
            firmAdapter.notifyDataSetChanged()

        }
    }
}